package com.prd.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.prd.dto.CustomerGuestPersonalDetailDTO;
import com.prd.mapper.CustomerGuestPersonalDetailMapper;
import com.prd.model.CustomerGuestPersonalDetailDO;
import com.prd.service.CustomerGuestPersonalDetailService;
import com.prd.tools.CopyNonNullBeanUtils;
import com.prd.util.JsonData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 客户-客人个人详情总表 服务实现类
 * </p>
 *
 * @author o1
 * @since 2023-12-14
 */
@Service("CustomerGuestPersonalDetailService")
public class CustomerGuestPersonalDetailServiceImpl extends ServiceImpl<CustomerGuestPersonalDetailMapper, CustomerGuestPersonalDetailDO> implements CustomerGuestPersonalDetailService {

    @Autowired
    private CustomerGuestPersonalDetailMapper personalDetailMapper;

    @Override
    public JsonData uUpdateAndAdd(CustomerGuestPersonalDetailDTO personalDetailDTO) {
        LambdaQueryWrapper<CustomerGuestPersonalDetailDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CustomerGuestPersonalDetailDO::getGuestNo, personalDetailDTO.getGuestNo());
        CustomerGuestPersonalDetailDO personalDetailDO = personalDetailMapper.selectOne(wrapper);
        if (personalDetailDO == null) {
            personalDetailDO = new CustomerGuestPersonalDetailDO();
            CopyNonNullBeanUtils.copyProperties(personalDetailDTO, personalDetailDO);
            personalDetailMapper.insert(personalDetailDO);
        }
        else {
            CopyNonNullBeanUtils.copyProperties(personalDetailDTO, personalDetailDO);
            personalDetailMapper.update(personalDetailDO, wrapper);
        }
        return JsonData.buildSuccess(personalDetailDO);
    }

    @Override
    public JsonData uSearch(String guestNo) {
        if (guestNo == null)
            return JsonData.buildError("guestNo参数为空");
        return JsonData.buildSuccess(search(guestNo));
    }

    @Override
    public CustomerGuestPersonalDetailDTO search(String guestNo) {
        LambdaQueryWrapper<CustomerGuestPersonalDetailDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CustomerGuestPersonalDetailDO::getGuestNo, guestNo);
        CustomerGuestPersonalDetailDO personalDetailDO = personalDetailMapper.selectOne(wrapper);
        CustomerGuestPersonalDetailDTO personalDetailDTO = new CustomerGuestPersonalDetailDTO();
        if (personalDetailDO != null)
            CopyNonNullBeanUtils.copyProperties(personalDetailDO, personalDetailDTO);
        return personalDetailDTO;
    }
}
